package models

import (
	"math"
	"strings"
	"time"

	"github.com/astaxie/beego"
	"github.com/astaxie/beego/orm"
	_ "github.com/go-sql-driver/mysql"
)

type StoreInfo struct {
	Id          int       `orm:"column(Id)"`
	Addr        string    `orm:"column(Address)"`
	Tel         string    `orm:"column(Tel)"`
	Name        string    `orm:"column(Name)"`
	Status      string    `orm:"column(Status)"`
	Opentime    string    `orm:"column(OpenTime)"`
	Closetime   string    `orm:"column(CloseTime)"`
	Img1        string    `orm:"column(HeadImg)"`
	Img2        string    `orm:"column(Img1)"`
	Img3        string    `orm:"column(Img2)"`
	Img4        string    `orm:"column(Img3)"`
	Trafficinfo string    `orm:"column(TrafficInfo)"`
	Position    string    `orm:"column(Position)"`
	UserId      int       `orm:"column(UserId)"`
	Createtime  time.Time `orm:"column(CreateTime)"`
	Staff       []*Statff `orm:"reverse(many)"`
}

func (p *StoreInfo) TableName() string {
	return "StoreInfo"
}

type StoreJson struct {
	Id          int    `json:"Id"`
	Addr        string `json:Address"`
	Tel         string `json:Tel"`
	Name        string `json:Name"`
	Status      string `json:Status"`
	Opentime    string `json:OpenTime"`
	Closetime   string `json:CloseTime"`
	Img1        string `json:HeadImg"`
	Img2        string `json:Img1"`
	Img3        string `json:Img2"`
	Img4        string `json:Img3"`
	Trafficinfo string `json:TrafficInfo"`
	Position    string `json:Position"`
}

func EarthDistance(lat1, lng1, lat2, lng2 float64) float64 {
	radius := 6371000 // 6378137
	rad := math.Pi / 180.0

	lat1 = lat1 * rad
	lng1 = lng1 * rad
	lat2 = lat2 * rad
	lng2 = lng2 * rad

	theta := lng2 - lng1
	dist := math.Acos(math.Sin(lat1)*math.Sin(lat2) + math.Cos(lat1)*math.Cos(lat2)*math.Cos(theta))

	return dist * float64(radius)
}

//查询门店
func QueryStores(userId int, location string) ([]StoreJson, error) {

	var stores []*StoreInfo

	o := orm.NewOrm()

	store := new(StoreInfo)
	acs := o.QueryTable(store)
	_, err := acs.Filter("Status", "active").Limit(50).All(&stores)
	if err != nil {
		beego.Error("acs active failed!", err)
		return nil, err
	}

	var result []StoreJson
	for _, v := range stores {

		newActive := StoreJson{
			Id:          v.Id,
			Addr:        v.Addr,
			Tel:         v.Tel,
			Name:        v.Name,
			Opentime:    v.Opentime,
			Closetime:   v.Closetime,
			Trafficinfo: v.Trafficinfo,
			Position:    v.Position,
			Img1:        v.Img1,
			Img2:        v.Img2,
			Img3:        v.Img3,
			Img4:        v.Img4,
		}

		result = append(result, newActive)
	}

	return result, nil
}

type StatffJson struct {
	Id       int    `josn:"Id"`
	NickName string `josn:"NickName"`
	Imag     string `josn:"Imag"`
	Role     string `josn:"Role"`    //1:店长，2:资深，3:学徒,0:Normal
	Product  string `josn:"Product"` //服务内容 1，2，3，4，5
}

type StoreDetail struct {
	Id          int          `json:"Id"`
	Name        string       `json:Name"`
	Status      string       `json:Status"`
	Opentime    string       `json:OpenTime"`
	Closetime   string       `json:CloseTime"`
	Tel         string       `json:Tel`
	Img1        string       `json:HeadImg"`
	Img2        string       `json:Img1"`
	Img3        string       `json:Img2"`
	Img4        string       `json:Img3"`
	Trafficinfo string       `json:TrafficInfo"`
	Position    string       `json:Position"`
	StatffNum   int          `json:"StaffNum"`
	Staffs      []StatffJson `json:Staff"`
}

func staffRoleConvert(product string) string {

	products := strings.Split(product, ",")

	res := ""
	for _, v := range products {
		switch v {
		case "1":
			res += "美甲,"
		case "2":
			res += "美睫,"
		case "3":
			res += "护肤,"
		case "4":
			res += "纹眉,"
		case "5":
			res += "小姐姐纹身,"
		}
	}
	return res
}

//浏览门店
func QueryStoresDetail(storeId int) (*StoreDetail, error) {

	store := StoreInfo{
		Id: storeId,
	}

	o := orm.NewOrm()

	err := o.Read(&store)
	if err != nil {
		beego.Error("load store failed!", err)
		return nil, err
	}

	num, err := o.LoadRelated(&store, "Staff")
	if err != nil {
		beego.Error("load staff failed!", err)
		return nil, err
	}

	StoreJson := StoreDetail{
		Id:          store.Id,
		Name:        store.Name,
		Opentime:    store.Opentime,
		Closetime:   store.Closetime,
		Img1:        store.Img1,
		Img2:        store.Img2,
		Img3:        store.Img3,
		Img4:        store.Img4,
		Trafficinfo: store.Trafficinfo,
		Tel:         store.Tel,
		Position:    store.Position,
		StatffNum:   int(num),
	}

	for i, _ := range store.Staff {

		if store.Staff[i].Active == "active" {

			staff := StatffJson{
				Id:       store.Staff[i].Id,
				NickName: store.Staff[i].NickName,
				Imag:     store.Staff[i].Imag,
				Role:     store.Staff[i].RoleDes,
				Product:  staffRoleConvert(store.Staff[i].Product),
			}
			StoreJson.Staffs = append(StoreJson.Staffs, staff)
		}
	}

	return &StoreJson, nil
}
